package com.hsource.paymentcenter.conf.wx;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/10 15:52
 * @describe 实现微信配置
 */
@Data
@Component
public class WechatConfig extends WXPayConfig {

    @Value("${wxpayconfig.appId}")
    private String appId;

    @Value("${wxpayconfig.mchId}")
    private String mchId;

    @Value("${wxpayconfig.deviceInfo}")
    private String deviceInfo;

    @Value("${wxpayconfig.key}")
    private String key;


    @Value("${wxpayconfig.notifyUrl}")
    private String notifyUrl;

    private byte[] certData;

    public WechatConfig() throws Exception {
        Resource resource = new ClassPathResource("apiclient_cert.p12");
        InputStream certStream = resource.getInputStream();
        this.certData = new byte[(int) resource.contentLength()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo(WXPayConstants.DOMAIN_API, false);
            }
        };
    }
}
